<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class StoresInfo extends Model
{
    //
    protected $table = 'stores_info';

    /**
     * 更新
     * @param array $data
     * @param $storeId
     * @return bool|void
     */
    public function saveOne(array $data, $storeId){
        $data['title'] = str_replace(" ", "", $data['title']);
        $data['title'] = str_replace(array("\r\n", "\r", "\n"), "", $data['title']);
        $data['store_id'] = $storeId;
        if( isset($data['id']) && $data['id'] ){
            $storeInfo = \DB::table('stores_info')->where('id', $data['id'])->select(['title', 'store_id'])->first();
            if( $storeInfo && $storeInfo->title != $data['title'] ){
                \DB::table('stores')->where('id', $storeInfo->store_id)->update(['wx_minisoft_path'=>"", "wx_minisoft_appid"=>"", 'updated_at'=>date('Y-m-d H:i:s')]);
            }
            \DB::table('stores_info')->where('id', $data['id'])->update(['title'=>$data['title'], 'updated_at'=>date('Y-m-d H:i:s')]);
        }else {
            \DB::table('stores_info')->insert([
                'title'=>$data['title'],
                'type'=>$data['type'],
                'uid'=>$data['uid'],
                'store_id'=>$data['store_id'],
                'created_at'=>date('Y-m-d H:i:s'),
                'updated_at'=>date('Y-m-d H:i:s')
            ]);
        }
    }
}
